home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
Z4PFCP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-26
|
6KB
|
221 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: z4pfcp.c
// Title: ZIP+4 Engine
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains the compressor for the POF file.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <z4.h>
//----------------------------------------------------------------------------
// Prototypes
//----------------------------------------------------------------------------
static BOOL FN_L Z4PFCompressAppend(PDATACOMP, PZ4_PF);
static SIZET FN_L Z4PFCompressZip3(PZ4_PF);
//----------------------------------------------------------------------------
// Description: Compression function
// Parameters: pdatacomp Compressor data
// Returns: Compression function result code. See data.h.
//----------------------------------------------------------------------------
SHORT FN_E Z4PFCompress(PDATACOMP pdatacomp)
{
static BOOL fPending; // ZIP5 record pending?
static Z4_PF pf; // ZIP5 record
static SIZET cMaxZips;
static SIZET cMaxZip3;
static CHAR szZip5[MAX_ZIP5+1];
static LONG lPofs;
static LONG lMultiPofs;
static LONG l2Pofs;
static SIZET cZip;
static SIZET cPof;
LONG lRec = pdatacomp->dlmrec.lId;
PPSZ ppsz = pdatacomp->dlmrec.apsz;
SIZET cZip3;
switch (lRec)
{
case DAI_INITIALIZE: // Initialize at startup
cMaxZips = 0;
cMaxZip3 = 0;
lPofs = 0;
l2Pofs = 0;
lMultiPofs = 0;
Assert(MAX_POF_ZIP5 <= pdatacomp->cbMax / MAX_ZIP5_BCD);
cZip = DataField(pdatacomp->dlmrec.pcfg, "zipcode");
cPof = DataField(pdatacomp->dlmrec.pcfg, "finance");
case DAI_START_BLK: // Start block
fPending = FALSE;
return DAO_SUCCESS;
case DAI_TERMINATE: // Terminate
Output("\n%ld POFs.\n", lPofs);
Output("%ld POFs w/ > 1 3 digit ZIP.\n", lMultiPofs);
Output("Maximum of %u ZIP5s in a POF.\n", cMaxZips);
Output("Maximum of %u ZIP3s in a POF.\n", cMaxZip3);
case DAI_FAILURE:
case DAI_END_BLK: // End block
return DAO_SUCCESS;
case DAI_LAST_REC:
if (!fPending)
return DAO_FAILURE;
Z4PFCompressAppend(pdatacomp, &pf);
lPofs++;
cZip3 = Z4PFCompressZip3(&pf);
cMaxZip3 = MAX(cMaxZip3, cZip3);
if (cZip3 > 1)
{
if (cZip3 == 2)
l2Pofs++;
lMultiPofs++;
}
return DAO_FLUSH;
}
if (pdatacomp->dlmrec.fSkipped)
return DAO_SKIP;
if (fPending)
{
if (strcmp(pf.szFinance, ppsz[cPof]) != 0)
{
Z4PFCompressAppend(pdatacomp, &pf);
lPofs++;
cZip3 = Z4PFCompressZip3(&pf);
cMaxZip3 = MAX(cMaxZip3, cZip3);
if (cZip3 > 1)
{
if (cZip3 == 2)
l2Pofs++;
lMultiPofs++;
}
fPending = FALSE;
return DAO_MARK_FLUSH;
}
if (strcmp(szZip5, ppsz[cZip]) != 0)
{
Assert(pf.cZip5 < MAX_POF_ZIP5);
strcpy(szZip5, ppsz[cZip]);
stra2b(pf.abZip5[pf.cZip5], MAX_ZIP5_BCD, ppsz[cZip], MAX_ZIP5);
pf.cZip5++;
cMaxZips = MAX(cMaxZips, pf.cZip5);
}
return DAO_NEXT;
}
fPending = TRUE;
memset(&pf, 0, sizeof(pf));
strcpy(pf.szFinance, ppsz[cPof]);
strcpy(szZip5, ppsz[cZip]);
stra2b(pf.abZip5[pf.cZip5], MAX_ZIP5_BCD, ppsz[cZip], MAX_ZIP5);
pf.cZip5++;
cMaxZips = MAX(cMaxZips, pf.cZip5);
return DAO_NEXT;
}
//----------------------------------------------------------------------------
// Description: Compress the current ZIP5 record
// Parameters: pdatacomp Compresser data
// ppf POF record
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
static BOOL FN_L Z4PFCompressAppend(PDATACOMP pdatacomp, PZ4_PF ppf)
{
PBYTE pb = pdatacomp->pb;
SIZET cb;
stra2b(pb, MAX_FINANCE_BCD, ppf->szFinance, MAX_FINANCE);
Assert(pb[0] != 0); // Expander will exit on a 0
pb += MAX_FINANCE_BCD;
*(PUSHORT)pb = (USHORT)ppf->cZip5; // Store ZIP count
pb += sizeof(USHORT);
cb = MAX_ZIP5_BCD * ppf->cZip5; // Store ZIPs
memcpy(pb, ppf->abZip5, cb);
pb += cb;
// Update number of bytes written
pdatacomp->cb = (SIZET)(pb - pdatacomp->pb);
return TRUE;
}
//----------------------------------------------------------------------------
// Description:
// Parameters: ppf POF record
// Returns: Number of 3 digit ZIP codes in a POF
//----------------------------------------------------------------------------
static SIZET FN_L Z4PFCompressZip3(PZ4_PF ppf)
{
CHAR szZip5a[MAX_ZIP5+1],szZip5b[MAX_ZIP5+1];
SIZET i, j, k;
szZip5a[MAX_ZIP5] = '\0';
szZip5b[MAX_ZIP5] = '\0';
k = 0;
for (i = 0; i < ppf->cZip5; ++i)
{
BOOL fDuplicate = FALSE;
strb2a(ppf->abZip5[i], MAX_ZIP5_BCD, szZip5a, MAX_ZIP5, TRUE);
for (j = 0; j < i; ++j)
{
strb2a(ppf->abZip5[j], MAX_ZIP5_BCD, szZip5b, MAX_ZIP5, TRUE);
if (memcmp(szZip5a, szZip5b, 3) == 0)
{
fDuplicate = TRUE;
break;
}
}
if (!fDuplicate)
k++;
}
return k;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------